package e3dds

import (
	"gitee.com/ameise84/e3dds/internal/message"
	"gitee.com/ameise84/e3dds/internal/rpc"
	"gitee.com/ameise84/e3pool/go_pool"
)

func takeActorRemote(name, sid, addr string) *actorRemote {
	return &actorRemote{
		name:      name,
		sid:       sid,
		addr:      addr,
		msgRunner: go_pool.NewGoRunner(nil, "actor remote", go_pool.DefaultOptions()),
	}
}

type actorRemote struct {
	c         *rpc.Conn
	name      string
	sid       string
	addr      string
	msgRunner go_pool.GoRunner
}

func (ts *actorRemote) send(msg *message.Msg) (err error) {
	if ts.c == nil {
		if ts.c, err = rpc.TakeRpcConn(ts.name, ts.addr); err != nil {
			return err
		}
	}

	if err = ts.c.Send(msg); err != nil {
		rpc.FreeRpcConn(ts.c)
		if ts.c, err = rpc.TakeRpcConn(ts.name, ts.addr); err != nil {
			return err
		}
		return ts.c.Send(msg)
	}
	return err
}
